package academic.ui.client.view;

import java.util.ArrayList;


import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ui.client.GenericContainerTag;
import academic.ui.client.config.ConfigConstants;
import academic.ui.client.presenter.PrimaryPresenter;

public class PrimaryView extends ViewImpl implements PrimaryPresenter.MyView{
		
	private GenericContainerTag mainBody = new GenericContainerTag("div");
	//private GenericContainerTag loginslot = new GenericContainerTag("div");
	private GenericContainerTag modalSlot = new GenericContainerTag("div");
	private GenericContainerTag gameSlot = new GenericContainerTag("div");

	private GenericContainerTag midDiv = new GenericContainerTag("div");
	private GenericContainerTag emailDiv = new GenericContainerTag("div");
	private ConfigConstants constants;
	private TextBox emailInput = new TextBox();
	private Button submitEmailButton = new Button("send me deals");
	private ArrayList<HasClickHandlers> prodNameLinks = new ArrayList<HasClickHandlers>();

	private static String DEFAULT_EMAIL_TEXT = "enter your email address...";
	@Inject
	public PrimaryView(ConfigConstants constants) {
		this.constants = constants;
		mainBody.add(modalSlot);
		//mainBody.add(loginslot);
		mainBody.add(midDiv);
		midDiv.setStyleName("midDiv");
		mainBody.setStyleName("primaryBody");
		gameSlot.setStyleName("gameSlot");
		modalSlot.setStyleName("modalSlot");
	}
	
	@Override
	public Widget asWidget() {
		return mainBody;
	}
	
	
	@Override
	public void setup() {
		midDiv.clear();
		emailDiv.clear();		
		midDiv.add(emailDiv);
		emailInput.getElement().setAttribute("spellcheck","false");
		emailDiv.add(emailInput);
		DOM.setStyleAttribute(emailDiv.getElement(), "visibility", "visible");
		emailDiv.setStyleName("emailDiv");
		emailInput.setStyleName("emailInput");
		emailInput.setFocus(false);
		emailInput.setText(DEFAULT_EMAIL_TEXT);
		emailInput.removeStyleName("typing");
		emailInput.addFocusHandler(new FocusHandler() {

			@Override
			public void onFocus(FocusEvent event) {
				if(DEFAULT_EMAIL_TEXT.equals(((TextBox)event.getSource()).getText()))
				{
					emailInput.setText("");
					emailInput.addStyleName("typing");
				}
			}
			
		});
		
		emailDiv.add(submitEmailButton);
		midDiv.add(gameSlot);
		
	}
	
	@Override
	public ArrayList<HasClickHandlers> getProductLinks() {
		return prodNameLinks;
	}

	@Override
	public void unBindView() {
		midDiv.clear();
	}
	
	@Override
	public void setInSlot(Object slotObj, Widget w) {
		if(slotObj==PrimaryPresenter.slot)
		{
			Log.debug("setInSlot: "+w.getStyleName());
			modalSlot.clear();
			if(w.getStyleName().equals("modal")) {
				modalSlot.add(w);
			}
			else {
			gameSlot.clear();
			gameSlot.add(w);
			}
			
		}
	}

	@Override
	public void removeEmailDiv() {
		DOM.setStyleAttribute(emailDiv.getElement(), "visibility", "hidden");
	}
	
	@Override
	public HasText emailInput() {
		return emailInput;
	}
	
	@Override
	public HasClickHandlers submitEmailButton() {
		return submitEmailButton;
	}
	

}
